<?php
class ControllerMenuMenu extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('common/header');
		$this->load->language('menu/menu');
			
		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/menu');

		$this->getList();
	}

	public function insert() {
		$this->load->language('common/header');
		$this->load->language('menu/menu');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/menu');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_menu_menu->addMenu($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('menu/menu', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('common/header');
		$this->load->language('menu/menu');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/menu');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_menu_menu->editMenu($this->request->get['menu_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('menu/menu', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getForm();
	}

	public function delete() {
		$this->load->language('common/header');
		$this->load->language('menu/menu');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/menu');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $menu_id) {
				$this->model_menu_menu->deleteMenu($menu_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('menu/menu', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getList();
	}

	private function getList() {
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}

		if (isset($this->request->get['filter_parent_menu'])) {
			$filter_parent_menu = $this->request->get['filter_parent_menu'];
		} else {
			$filter_parent_menu = '';
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
		);
		
		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_restaurant'),
			'href'      => '#',
      		'separator' => ' :: '
		);
		
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_menu'),
			'href'      => '#',
			'separator' => ' :: '
		);
				
		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => '#',       		
      		'separator' => ' :: '
		);

		$this->data['insert'] = $this->url->link('menu/menu/insert', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		$this->data['delete'] = $this->url->link('menu/menu/delete', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		 
		$this->data['menus'] = array();

		$page = isset($this->request->get['page']) ? $this->request->get['page'] : 1;
		$sort = isset($this->request->get['sort']) ? $this->request->get['sort'] : 'md.name';
		$order = isset($this->request->get['order']) ? $this->request->get['order'] : 'ASC';

		$data = array(
			'filter_name'	  => $filter_name, 
			'filter_parent_menu'	  => $filter_parent_menu,
			'filter_status'   => $filter_status,
			'sort'            => $sort,
			'order'           => $order,
			'start'      	  => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'  	      => $this->config->get('config_admin_limit')
		);
		
		$this->load->model('setting/image');

		$menu_menu_total = $this->model_menu_menu->getTotalMenus($data);
		 
		$results = $this->model_menu_menu->getMenus($data);

		$this->load->model('restaurant/restaurant');

		foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('menu/menu/update', 'token=' . $this->session->data['token'] . '&menu_id=' . $result['menu_id'] . $this->url(), 'SSL')
			);

			if (isset($result['image']) && !empty($result['image']) && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_setting_image->resize($result['image'], 100, 100);
			} else {
				$image = $this->model_setting_image->resize('no_image.jpg', 100, 100);
			}

			$this->data['menus'][] = array(
				'menu_id' => $result['menu_id'],
				'name'        => $this->model_menu_menu->getPath($result['menu_id']),
				'sort_order'  => $result['sort_order'],
				'image'		  => $image,
				'status'      => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'selected'    => isset($this->request->post['selected']) && in_array($result['menu_id'], $this->request->post['selected']),
				'action'      => $action
			);
		}

		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());
		 
		$this->data['token'] = $this->session->data['token'];

		if (!isset($this->error['warning'])) {
			$this->data['error_warning'] = '';
		}
			
		if (isset($this->error['menu_in_use'])) {
			$this->data['error_menu_in_use'] = $this->error['menu_in_use'];
		} else {
			$this->data['error_menu_in_use'] = array();
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$this->data['parent_menus'] = $this->model_menu_menu->getMenusByParentId(0);

		$this->data['sort_name'] = $this->url->link('menu/menu', 'token=' . $this->session->data['token'] . $this->url('md.name'), 'SSL');
		$this->data['sort_status'] = $this->url->link('menu/menu', 'token=' . $this->session->data['token'] . $this->url('m.status'), 'SSL');
		$this->data['sort_sort_order'] = $this->url->link('menu/menu', 'token=' . $this->session->data['token'] . $this->url('m.sort_order'), 'SSL');
		
		$pagination = new Pagination();
		$pagination->total = $menu_menu_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('menu/menu', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		 
		$this->data['pagination'] = $pagination->render();
		
		
		$this->data['filter_name'] = $filter_name;
		$this->data['filter_parent_menu'] = $filter_parent_menu;
		$this->data['filter_status'] = $filter_status;

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'menu/menu_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function getForm() {
		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());
			
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
	
 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
   			'text'      => $this->language->get('text_restaurant'),
   			'href'      => '#',
   			'separator' => ' :: '
   		);
   				
   		$this->data['breadcrumbs'][] = array(
   			'text'      => $this->language->get('text_menu'),
   			'href'      => '#',
			'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('menu/menu', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		if (!isset($this->request->get['menu_id'])) {
   			$this->data['breadcrumbs'][] = array(
	       		'text'      => $this->language->get('heading_title_insert'),
				'href'      => '#',
	      		'separator' => ' :: '
			);
   		} else {
   			$this->data['breadcrumbs'][] = array(
	       		'text'      => $this->language->get('heading_title_update'),
				'href'      => '#',
	      		'separator' => ' :: '
			);
   		}
		
		if (!isset($this->request->get['menu_id'])) {
			$this->data['action'] = $this->url->link('menu/menu/insert', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		} else {
			$this->data['action'] = $this->url->link('menu/menu/update', 'token=' . $this->session->data['token'] . '&menu_id=' . $this->request->get['menu_id'] . $this->url(), 'SSL');
		}
		
		$this->data['cancel'] = $this->url->link('menu/menu', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->request->get['menu_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$menu_info = $this->model_menu_menu->getMenu($this->request->get['menu_id']);
    	}
		
		$this->load->model('setting/language');
		
		$this->data['languages'] = $this->model_setting_language->getLanguages(array('supported' => $this->restaurant->getConfig()->get('config_supported_language')));

		if (isset($this->request->post['menu_description'])) {
			$this->data['menu_description'] = $this->request->post['menu_description'];
		} elseif (isset($this->request->get['menu_id'])) {
			$this->data['menu_description'] = $this->model_menu_menu->getMenuDescriptions($this->request->get['menu_id']);
		} else {
			$this->data['menu_description'] = array();
		}
		
		$menus = $this->model_menu_menu->getMenusByParentId(0);
		 
		// Remove own id, descendant id from list
		if (!empty($menu_info)) {
			foreach ($menus as $key => $menu) {
				if ($menu['menu_id'] == $menu_info['menu_id']) {
					unset($menus[$key]);
				} else if($this->model_menu_menu->isDescendant($menu_info['menu_id'], $menu['menu_id'])){
					unset($menus[$key]);
				}
			}
		}

		$this->data['menus'] = $menus;

		if (isset($this->request->post['parent_id'])) {
			$this->data['parent_id'] = $this->request->post['parent_id'];
		} elseif (!empty($menu_info)) {
			$this->data['parent_id'] = $menu_info['parent_id'];
		} else {
			$this->data['parent_id'] = 0;
		}
		
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (!empty($menu_info)) {
			$this->data['keyword'] = $menu_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}

		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (!empty($menu_info)) {
			$this->data['image'] = $menu_info['image'];
		} else {
			$this->data['image'] = '';
		}
		
		$this->load->model('setting/image');

		if (!empty($menu_info) && $menu_info['image'] && file_exists(DIR_IMAGE . $menu_info['image'])) {
			$this->data['thumb'] = $this->model_setting_image->resize($menu_info['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		}
		
		$this->data['no_image'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		
		if (isset($this->request->post['top'])) {
			$this->data['top'] = $this->request->post['top'];
		} elseif (!empty($menu_info)) {
			$this->data['top'] = $menu_info['top'];
		} else {
			$this->data['top'] = 0;
		}
		
		if (isset($this->request->post['column'])) {
			$this->data['column'] = $this->request->post['column'];
		} elseif (!empty($menu_info)) {
			$this->data['column'] = $menu_info['column'];
		} else {
			$this->data['column'] = 1;
		}
				
		if (isset($this->request->post['sort_order'])) {
			$this->data['sort_order'] = $this->request->post['sort_order'];
		} elseif (!empty($menu_info)) {
			$this->data['sort_order'] = $menu_info['sort_order'];
		} else {
			$this->data['sort_order'] = 0;
		}
		
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (!empty($menu_info)) {
			$this->data['status'] = $menu_info['status'];
		} else {
			$this->data['status'] = 1;
		}

		$this->template = 'menu/menu_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'menu/menu')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		foreach ($this->request->post['menu_description'] as $language_id => $value) {
			if ((utf8_strlen($value['name']) < 2) || (utf8_strlen($value['name']) > 255)) {
				$this->error['name'][$language_id] = $this->language->get('error_name');
			}
		}
		
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
					
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'menu/menu')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		$this->load->model('setting/setting');
		$this->load->model('menu/food');

		// Check if any menu has child menu or food
		foreach ($this->request->post['selected'] as $menu_id) {
			$has_decendant = $this->model_menu_menu->hasDecendant($menu_id);
			
			if($has_decendant) {
				$this->error['menu_in_use'][$menu_id] = $this->language->get('error_menu_in_use');
			} else if(sizeof($this->model_menu_food->getFoodsByMenuId($menu_id)) > 0){
				$this->error['menu_in_use'][$menu_id] = $this->language->get('error_menu_in_use');				
			}
		}

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function url($sort = ''){
		$url = "";

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}

		if (isset($this->request->get['filter_parent_menu'])) {
			$url .= '&filter_parent_menu=' . $this->request->get['filter_parent_menu'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		if(!empty($sort)) {
			if ($order == 'ASC') {
				$url .= '&order=DESC';
			} else {
				$url .= '&order=ASC';
			}

			$url .= '&sort=' . $sort;
		} else {

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			} else {
				$url .= '&sort=md.name';
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
		}

		$url .= '&page=' . $page;

		return $url;
	}
}
?>